<?php
declare (strict_types = 1);

namespace app\xiangji\command;

use app\xiangji\caches\ScoreRankCache;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class CacheScoreRank extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('cache:love_score_rank')
            ->setDescription('生成并缓存用户恩爱值排行榜');
    }

    protected function execute(Input $input, Output $output)
    {
        $binds = ScoreRankCache::getAllLoverBind();
        if (!$binds) {
            $output->writeln('暂无恋人绑定数据');
            return;
        };
        foreach ($binds as $user_id => $lover_id){
            if ($user_id == 0 || $lover_id == 0) continue; // 未绑定关系不上榜
            $bind_score = ScoreRankCache::getLoversScore($user_id, $lover_id);
            ScoreRankCache::setScore(implodeUserId($user_id, (int)$lover_id), array_sum($bind_score));
        }
    	// 指令输出
    	$output->writeln('生成并缓存用户恩爱值排行榜成功');
    }
}
